package com.kara.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.appfuse.model.BaseObject;
import org.appfuse.model.User;

@Entity
@Table(name = "class")
@AttributeOverride(name="schoolYear_id", column=@Column(name="school_year_id"))
public class Clazz implements Serializable {

	private Long id;
	private String name;
	private Trinn trinn;
	private SchoolYear schoolYear;
	private User mainTeacher; 
	private List<User> teachers; 
	private List<Student> students;
	
	@Id
	@SequenceGenerator(name = "EMP_SEQ_GEN", sequenceName = "emp_sequence")
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EMP_SEQ_GEN")
	public Long getId() {
		return id;
	}
	
	@Column(nullable=false, length=255)
	public String getName() {
		return name;
	}
	
	@Enumerated(EnumType.STRING)
	public Trinn getTrinn() {
		return trinn;
	}
	
	@ManyToOne(fetch=FetchType.LAZY, targetEntity=SchoolYear.class)
	public SchoolYear getSchoolYear() {
		return schoolYear;
	}

	@OneToOne()
	public User getMainTeacher() {
		return mainTeacher;
	}

	@Column(name="other_teacher")
	@OneToMany(fetch=FetchType.LAZY)
	public List<User> getTeachers() {
		if (teachers == null){
			return new ArrayList<User>();
		}
		return teachers;
	}
	
	@Column(name="student")
	@OneToMany(fetch=FetchType.EAGER, targetEntity=Student.class, orphanRemoval=true)
	public List<Student> getStudents() {
		
		if (students == null){
			return new ArrayList<Student>();
		}
		
		return students;
	}

	public void setStudents(List<Student> students) {
		this.students = students;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setSchoolYear(SchoolYear schoolYear) {
		this.schoolYear = schoolYear;
	}

	public void setTeachers(List<User> teachers) {
		this.teachers = teachers;
	}

	public void setTrinn(Trinn trinn) {
		this.trinn = trinn;
	}
	
	public void setMainTeacher(User mainTeacher) {
		this.mainTeacher = mainTeacher;
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE)
				.append("id", id).append("name", name).append("trinn", trinn)
				.append("semester", schoolYear.toString()).toString();
	}
	public boolean hasNotMainTeacher(){
		if (mainTeacher == null){
			return true;
		}
		return false;
	}
	@Override
	public boolean equals(Object o) {
		if (o == this) {
			return true;
		}
		if (!(o instanceof Clazz)) {
			return false;
		}
		Clazz oClazz = (Clazz) o;

		if (oClazz.getId() == null){
			return false;
		}
		if (oClazz.getId().equals(this.getId())) {
			return true;
		}
		else {
			return false;
		}
	}
	@Override
	public int hashCode() {
		return this!= null ? this.id.hashCode(): 0;
	}
}
